package com.ciwong.epaper.modules.clazz.ui;

import android.content.Intent;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.application.EpaperConstant;
import com.ciwong.epaper.modules.cordva.BaseHtmlActicity;
import com.ciwong.epaper.modules.me.util.MeJumpManager;
import com.ciwong.epaper.util.HttpRequest;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.epaper.util.RequestCode;
import com.ciwong.libs.utils.volley.BaseRequest;

import java.net.URLEncoder;

/**
 * @description: 作业报告页面
 * @author: wyk
 * @time: 2022/2/15 15:07
 */
public class WorkReportActivity extends BaseHtmlActicity {
    //班级id
    private String classId;
    //班级名
    private String className;

    private String TAG;

    @Override
    protected void preCreate() {
        super.preCreate();
        Intent intent = getIntent();
        classId = intent.getStringExtra(IntentFlag.INTENT_FLAG_CLASS_ID);
        className = intent.getStringExtra(IntentFlag.INTENT_FLAG_CLASS_NAME);
        TAG = intent.getStringExtra(IntentFlag.INTENT_FLAG_TAG);
    }

    @Override
    protected void init() {
        super.init();
        if (TAG.equals(RequestCode.TeaWorkContent.WORK_STATISTICS_REPORT)) {
            setTitleText(R.string.work_statistics_report);
        } else if (TAG.equals(RequestCode.TeaWorkContent.WORK_LISTENSPEAK_REPORT)) {
            setTitleText(R.string.work_listen_speak_report);
        } else if (TAG.equals(RequestCode.TeaWorkContent.WORK_EXAM_ROOM_REPORT)) {
            setTitleText(R.string.work_exam_room_report);
        } else if (TAG.equals(RequestCode.TeaWorkContent.STUDENT_WORK)) {
            setTitleText(R.string.student_work_report);
        }
        BaseRequest.VerifyInfo verifyInfo = HttpRequest.getVerifyInfo();
        if (null != verifyInfo) {
            String clientId = verifyInfo.getClientId();
            String accessToken = verifyInfo.getAccessToken();
            String brandId = EApplication.BRAND_ID + "";
            long userId = 0;
            if (null != EApplication.getInstance().getUserInfoBase()) {
                userId = EApplication.getInstance().getUserInfoBase().getUserId();
            }
            className = toUtf8(className);
            String startURL;
            if (TAG.equals(RequestCode.TeaWorkContent.COMPUTER_ROOM_EXAM)) {
                startURL = EpaperConstant.URL_COMPUTER_ROOM_TEST_REPORT_H5 + "clientId=" + clientId + "&accessToken=" + accessToken + "&brandId=" + brandId + "&classId=" + classId + "&className=" + className;
            } else if (TAG.equals(RequestCode.TeaWorkContent.STUDENT_WORK)) {
                startURL = EpaperConstant.URL_STUDENT_WORK_REPORT_H5 + "clientId=" + clientId + "&accessToken=" + accessToken + "&brandId=" + brandId + "&classId=" + classId + "&className=" + className;
            } else if (TAG.equals(RequestCode.TeaWorkContent.CLASS_WORK)) {
                startURL = EpaperConstant.URL_CLASSROOM_TEACHING_REPORT_H5 + "clientId=" + clientId + "&accessToken=" + accessToken + "&brandId=" + brandId + "&classId=" + classId + "&className=" + className;
            } else if (TAG.equals(RequestCode.TeaWorkContent.HOME_WORK)) {
                startURL = EpaperConstant.URL_HOMEWORK_REPORT_H5 + "clientId=" + clientId + "&accessToken=" + accessToken + "&brandId=" + brandId + "&classId=" + classId + "&className=" + className;
            } else if (TAG.equals(RequestCode.TeaWorkContent.WORK_LISTENSPEAK_REPORT)) {
                //跳转听说分析报告
                startURL = EpaperConstant.URL_CLASS_WORK_REPORT_H5 + "reportType=2&clientId=" + clientId + "&accessToken=" + accessToken + "&brandId=" + brandId + "&classId=" + classId + "&userId=" + userId;
            } else if (TAG.equals(RequestCode.TeaWorkContent.WORK_EXAM_ROOM_REPORT)) {
                //跳转机房考试报告
                startURL = EpaperConstant.URL_CLASS_WORK_REPORT_H5 + "reportType=3&clientId=" + clientId + "&accessToken=" + accessToken + "&brandId=" + brandId + "&classId=" + classId + "&userId=" + userId;
            } else {
                //跳转作业统计报告
                startURL = EpaperConstant.URL_CLASS_WORK_REPORT_H5 + "reportType=1&clientId=" + clientId + "&accessToken=" + accessToken + "&brandId=" + brandId + "&classId=" + classId + "&userId=" + userId;
            }
            setStartURL(startURL);
            loadUrl(startURL.toString());
        } else {
            showToastError(getString(R.string.get_user_info_error));
            finish();
        }
    }

    //将中文转为utf-8
    public static String toUtf8(String str) {
        String result = null;
        try {
            result = URLEncoder.encode(str, "UTF-8");
            result = result.replace("+", "%20");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }


    @Override
    protected void loadData() {
    }

    @Override
    protected void initEvent() {
        super.initEvent();
    }

    @Override
    protected void onStart() {
        super.onStart();
        //EventBus.getDefault().register(this);
    }


    @Override
    protected void onStop() {
        super.onStop();
        //EventBus.getDefault().unregister(this);
    }

    @Override
    public void finish() {
        if (webView.canGoBack()) {
            webView.goBack();
            return;
        }
        super.finish();
    }

    /**
     * 跳入h5内部资源
     *
     * @param urlH5
     */
    public void jumpToInnerResouce(String urlH5) {
        if (!urlH5.contains("http")) {//解决连接中不包含http 导致闪退的bug
            urlH5 = "http://" + urlH5;
        }
        MeJumpManager.jumpToResourseCenterInnerH5Activity(R.string.go_back, this, urlH5);

    }
}
